//
//  String+Verify.swift
//  casdon_swiftUI
//
//  Created by ww on 2025/3/30.
//

import Foundation

extension String {
    // Validate phone number
    var isPhoneNum: Bool {
        let phoneRegex = "^1[3-9]\\d{9}$"
        let phoneTest = NSPredicate(format: "SELF MATCHES %@", phoneRegex)
        return phoneTest.evaluate(with: self)
    }

    // Validate ID card number (example for Chinese ID card)
    var isIDCardNum: Bool {
        let idCardRegex = "^[0-9]{15}|[0-9]{17}[0-9Xx]$"
        let idCardTest = NSPredicate(format: "SELF MATCHES %@", idCardRegex)
        return idCardTest.evaluate(with: self)
    }

    // Validate email address
    var isEmail: Bool {
        let emailRegex = "^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}$"
        let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegex)
        return emailTest.evaluate(with: self)
    }
    
    // 密码至少8位
    // 字母 + 数字
    // 数字 + 特殊字符
    // 字母 + 特殊字符  
    // 字母 + 数字 + 特殊字符
    var isValidPassword: Bool {
       let passwordRegex = "^(?=.*[a-zA-Z])(?=.*\\d)[a-zA-Z\\d]{8,}$|^(?=.*\\d)(?=.*[@$!%*?&])[\\d@$!%*?&]{8,}$|^(?=.*[a-zA-Z])(?=.*[@$!%*?&])[A-Za-z@$!%*?&]{8,}$|^(?=.*[a-zA-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,}$"
        let passwordTest = NSPredicate(format: "SELF MATCHES %@", passwordRegex)
        return passwordTest.evaluate(with: self)
    }
}
